#include "recboxitem.h"
#include "ui_recboxitem.h"

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);
    //给对应的ui安装上事件拦截器
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    //实现鼠标进入该界面的时候当前的界面向上移动，出去的时候该界面向下还原到原来的位置
    if(watched == ui->musicImageBox)
    {
        int ImgWidth = ui->musicImageBox->width();
        int ImgHeight = ui->musicImageBox->height();
        //分成进入和离开两种情况
        if(event->type() == QEvent::Enter)
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setDuration(100);
            animation->setStartValue(QRect(9,9,ImgWidth,ImgHeight));
            animation->setEndValue(QRect(9,0,ImgWidth,ImgHeight));
            animation->start();
            //处理后续的信号，这里因为是new的，可以在处理后续的信号中，把该动画对象释放掉
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
                //qDebug() << "完成动画上移动作";
            });
            return true;
        }
        else if(event->type() == QEvent::Leave)
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setDuration(100);
            animation->setStartValue(QRect(9,0,ImgWidth,ImgHeight));
            animation->setEndValue(QRect(9,9,ImgWidth,ImgHeight));
            animation->start();
            //处理后续的信号，这里因为是new的，可以在处理后续的信号中，把该动画对象释放掉
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
                //qDebug() << "完成动画下移动作";
            });
            return true;
        }
    }
    //如果不是该对象，就继续执行后面的处理
    return QObject::eventFilter(watched,event);
}

void RecBoxItem::setImage(const QString &path)
{
    QString imgStyle = "border-image:url(" + path + ");";
    ui->recMusicImage->setStyleSheet(imgStyle);
}

void RecBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}
